#!/usr/bin/ruby
require 'gtk2'
require 'libglade2'
require 'JamendoMethod'
Gtk.init


#dichiaro le varibili
art = "_"
player = " "
#apro il file
file = File.new 'name.txt'
line = file.readlines
#2 comandi da shell
system("mkdir ~/.jamendo")
system("touch ~/.jamendo/impostazioni.txt")

@glade = GladeXML.new('jamendo.glade')
@glade.widget_names.each do |name|
      instance_variable_set("@#{name}".intern, @glade[name])
end
#qui per la combobox
line[0..4433].each do |s|

  @combobox1.insert_text(1, s)
end

#qui il  menu
@quit.signal_connect('activate') do
  Gtk.main_quit
end

#qui bottone primo riquadro tabella da sinistra
#qui per non far avviare il primo bottone ho messo tutti dentro il primo e il secondo
#E' rude però funziona :P
@button1.signal_connect('clicked') do
  @label16.set_text(" ")
  puts @combobox1.active_text
  art =  @combobox1.active_text
  if art == nil
  else
    puts "valore variabile art: #{art}"
    JamendoMethod::album(art, @label2, @label3)
    
    @button2.signal_connect('clicked') do
        @label16.set_text(" ")
      spinbutton_value =  @spinbutton1.value
      spin_value = spinbutton_value.to_i
      JamendoMethod::get_id(art, spin_value)
      home = `echo $HOME`.chop
      play = File.new("#{home}/.jamendo/impostazioni.txt")
      chose =  play.readlines
      puts chose[1]
      if chose[1] == "si\n"
	JamendoMethod::get_info(art, @label16)
      else
      end
      JamendoMethod::playlist(art, @label5, @label6)
      
      @button3.signal_connect('clicked') do
	JamendoMethod::m3u(art)
	@button5.signal_connect('clicked') do
	  home = `echo $HOME`.chop
	  play = File.new("#{home}/.jamendo/impostazioni.txt")
	  chose =  play.readlines
	  JamendoMethod::player(chose[0].chop, art)
	end
      end
      
      @button4.signal_connect('clicked') do
	puts "not downloaded"
      end
      
      @button6.signal_connect('clicked') do
	JamendoMethod::artwork(art, @image1)
	
      end
      
      @button8.signal_connect('clicked') do
	puts 'not downloaded'
      end
      
      @button10.signal_connect('clicked') do
	JamendoMethod::torrent(art)
      end
      @button11.signal_connect('clicked') do
	puts 'not downloaded'
      end
    end
  end
end
@window1.signal_connect('destroy') do
  Gtk.main_quit
end
@info.signal_connect('activate') do
  Thread.new do
    system("ruby info.rb")
  end
end
@Impostazioni.signal_connect('activate') do
  Thread.new do
    system("ruby impo.rb")
  end
end
@Download.signal_connect('activate') do
  @window2.show_all
  @button7.signal_connect('clicked') do
    puts @spinbutton2.value
    value = @spinbutton2.value.to_i
    JamendoMethod::dumpstream(art, value, @label13, @window3)
  end
end
@window1.show_all


Gtk.main


